'use strict';


exports.main = async (event, context) => {
	//event为客户端上传的参数
	 
	const md5 = require('md5-node'); // 调用接口需要引入MD5模块。安装模块：npm install md5-node --save
	const ACCOUNT_SID = '96c6b2dad312445b9c511066eedf632c'; // 开发者ACCOUNT_SID，请登录sms.miaodiyun.com 获取
	const AUTH_TOKEN = '7b14d443324048ea8eddc8ef524badb0'; // 开发者AUTH_TOKEN，请登录sms.miaodiyun.com 获取
	const URL = 'https://openapi.miaodiyun.com/distributor/sendSMS'; // 接口域名
	const to = event.phone; // 发送手机号，多个手机号，用英文逗号隔开
	const templateid = '216923'; // 模板ID
	const code = '';
	var num  = Math.floor(Math.random()*(9999-1000))+1000;
	const param = `${num},2`; // 短信变量
	const timestamp = new Date().getTime(); // 时间戳
	const sig = md5(ACCOUNT_SID + AUTH_TOKEN + timestamp);
	const data =
		`?accountSid=${ACCOUNT_SID}&to=${to}&templateid=${templateid}&param=${param}&timestamp=${timestamp}&sig=${sig}`;

	const url = URL
	const res = await uniCloud.httpclient.request(url, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		},
		dataType: "json",
		data: {
			accountSid: ACCOUNT_SID,
			templateid,
			to,
			sig,
			param,
			timestamp
		}
	})
	//返回数据给客户端 
	return {
		state:res,
		code:num
	}
};
